package com.asohofrucol.web;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.event.ActionEvent;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AgrarianResourceDTO;
import com.asohofrucol.dto.AgrarianResourceRegisterDTO;
import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.dto.AssociativityDTO;
import com.asohofrucol.dto.AssociativityRegisterDTO;
import com.asohofrucol.dto.ForestResourceDTO;
import com.asohofrucol.dto.ForestResourceRegisterDTO;
import com.asohofrucol.dto.LivestockResourceDTO;
import com.asohofrucol.dto.LivestockResourceRegisterDTO;
import com.asohofrucol.dto.ProcessTransferInnovationDTO;
import com.asohofrucol.dto.ProductPriorityDTO;
import com.asohofrucol.dto.RuatRegisterDTO;
import com.asohofrucol.dto.SurveyDTO;
import com.asohofrucol.service.UserOwnerService;

public class RuatDataController {
	@Autowired
	private UserOwnerService userOwnerService;

	@Autowired
	private AsohofrucolSessionDTO aSession;


	private Map<String,Long> options  =new LinkedHashMap<String, Long>(); 
	
	public void saveUserOwnerData(ActionEvent actionEvent) {
		System.out.println("saveUserOwnerData");
		RequestContext context = RequestContext.getCurrentInstance();
		context.addCallbackParam("view", "result_data.xhtml");
	}

	public void saveRuatRegister(ActionEvent actionEvent) {
		RuatRegisterDTO ruatRegisterDTO = aSession.getSelectedRuatRegister();
		String userModifier = aSession.getUserInfo().getUserName();
		ruatRegisterDTO = userOwnerService.saveRuatRegisterDTO(ruatRegisterDTO,
				userModifier);
		if (ruatRegisterDTO != null) {
			aSession.setSelectedRuatRegister(ruatRegisterDTO);
		}
	}

	public void saveAssocitivities(ActionEvent actionEvent) {
		AssociativityDTO associativityDTO = aSession.getAssociativity();
		userOwnerService.saveAssociativityDTO(associativityDTO, aSession.getUserInfo().getUserName());
	}

	public void cancelRuatRegister(ActionEvent actionEvent) {
		RuatRegisterDTO ruatRegisterDTO = new RuatRegisterDTO();
		ruatRegisterDTO.setStatus("0");
		aSession.setSelectedRuatRegister(ruatRegisterDTO);
		aSession.getRuat().getRuatRegisters().clear();
	}

	public void loadAgrarianBusinessData() {
		Long ruatId = aSession.getSelectedRuatRegister().getId();

		AgrarianResourceDTO agrarianResource = userOwnerService
				.getAgrarianResourceDTOByRuatId(ruatId);
		if (agrarianResource == null) {
			agrarianResource = new AgrarianResourceDTO();		
		}
		aSession.setAgrarianResource(agrarianResource);
	}
	
	public void loadAgrarianBusinessData(ActionEvent actionEvent) {
		this.loadAgrarianBusinessData();
	}

	public void loadAnimalBusinessData() {
		Long ruatId = aSession.getSelectedRuatRegister().getId();

		LivestockResourceDTO liveStockResource = userOwnerService
				.getLivestockResourceDTOByRuatId(ruatId);
		if (liveStockResource == null) {
			liveStockResource = new LivestockResourceDTO();
		}

		aSession.setLivestockResource(liveStockResource);
	}	
	
	public void loadAnimalBusinessData(ActionEvent actionEvent) {
		this.loadAnimalBusinessData();
	}

	public void loadForestBusinessData(ActionEvent actionEvent) {
		this.loadForestBusinessData();
	}
	public void loadForestBusinessData() {
		Long ruatId = aSession.getSelectedRuatRegister().getId();
		ForestResourceDTO forestResource = userOwnerService
				.getForestResourceDTOByRuatId(ruatId);
		if (forestResource == null) {
			forestResource = new ForestResourceDTO();
		}
		aSession.setForestResource(forestResource);
	}

	public void saveForestBusinessData(ActionEvent actionEvent) {
		ForestResourceDTO forestResourceDTO = aSession.getForestResource();
		for (ForestResourceRegisterDTO frr : forestResourceDTO
				.getForestResourceRegisters()) {
			frr.setRuatId(aSession.getSelectedRuatRegister().getId());
		}
		userOwnerService.saveForestResource(forestResourceDTO, aSession.getUserInfo().getUserName());
	}

	public void saveAnimalBusinessData(ActionEvent actionEvent) {
		LivestockResourceDTO livestockResourceDTO = aSession
				.getLivestockResource();
		for (LivestockResourceRegisterDTO lsr : livestockResourceDTO
				.getLivestockResourceRegisters()) {
			lsr.setRuatId(aSession.getSelectedRuatRegister().getId());
		}
		userOwnerService.saveLivestockResource(livestockResourceDTO, aSession.getUserInfo().getUserName());
	}
	
	public void saveAgrarianBusinessData(ActionEvent actionEvent) {
		
		AgrarianResourceDTO agrarianResourceDTO=aSession.getAgrarianResource();
		
		for (AgrarianResourceRegisterDTO agr : agrarianResourceDTO.getAgrarianResourceRegisters()) {
			agr.setRuatId(aSession.getSelectedRuatRegister().getId());
		}
		
		
		userOwnerService.saveAgrarianResourceDTO(agrarianResourceDTO, aSession.getUserInfo().getUserName());

		
	}

	public void loadAssociativitytBusinessData(ActionEvent actionEvent) {
		Long ruatId = aSession.getSelectedRuatRegister().getId();
		AssociativityDTO associativity = userOwnerService
				.getAssociativityDTOByRuatId(ruatId);
		if (associativity == null) {
			associativity = new AssociativityDTO();
			AssociativityRegisterDTO associativityRegisterDTO = new AssociativityRegisterDTO();
			associativity.getAssociativityRegisters().add(
					associativityRegisterDTO);
		}
		aSession.setAssociativity(associativity);
	}

	public void deleteRuat(ActionEvent actionEvent) {
		Long ruatId = aSession.getSelectedRuatRegister().getId();
		String userModifier = aSession.getUserInfo().getUserName();
		userOwnerService.deleteRuat(ruatId, userModifier);
	}

	public void loadSurvey(ActionEvent actionEvent) {
		Long ruatId = aSession.getSelectedRuatRegister().getId();
		SurveyDTO  survey = userOwnerService.getSurveyByRuatId(ruatId);
		if(survey==null){
			survey = new SurveyDTO();
			survey.setStatus("1");
		}
		aSession.setSurvey(survey);
	}

	public void saveSurvey(ActionEvent actionEvent) {
		SurveyDTO  survey = aSession.getSurvey();
		survey.setRuatId(aSession.getSelectedRuatRegister().getId());
		userOwnerService.saveSurvey(survey, aSession.getUserInfo().getUserName());	
	}
	
	public void loadPriorityProduct(){
		Long ruatId = aSession.getSelectedRuatRegister().getId();
		List<ProductPriorityDTO> ppDto= userOwnerService.getProductPriorityByRuatId(ruatId);
		int sizeProducts = getIdsProducts().size();
		int sizeRegisters = ppDto.size();
		for(;sizeRegisters<sizeProducts; sizeRegisters++){
			ProductPriorityDTO ppDTO = new ProductPriorityDTO();
			ppDTO.setPosition(Long.valueOf(sizeRegisters+1));
			ppDTO.setRuatId(ruatId);
			ppDTO.setProductId(1L);
			ppDto.add(ppDTO);
		}		
		aSession.setProductPriority(ppDto);
		aSession.setOptionsProducts(options);
	}
	
	public void loadPriorityProduct(ActionEvent actionEvent){
		this.loadPriorityProduct();
	}

	public void savePriorityProduct(ActionEvent actionEvent) {
		List<ProductPriorityDTO> ppDto = aSession.getProductPriority();	
		userOwnerService.saveProductPriority(ppDto, aSession.getUserInfo().getUserName());
	}
	
	public void saveProcessTransferInnovation(ActionEvent actionEvent) {

		userOwnerService.saveProcessTransferInnovationDTO(aSession.getProcessTransferInnovation(), aSession.getUserInfo().getUserName());
		
	}

	public void loadProcessTransferInnovationData(ActionEvent actionEvent) {
		ProcessTransferInnovationDTO ptiDto = userOwnerService.getProcessTransferInnovationDTOByRuatId(aSession.getSelectedRuatRegister().getId());
		aSession.setProcessTransferInnovation(ptiDto);
	}

	public Boolean registerAlreadyExisist(ActionEvent actionEvent){
		RuatRegisterDTO ruatRegisterDTO = aSession.getSelectedRuatRegister();
		return userOwnerService.registerAlreadyExisist(ruatRegisterDTO);
	}
	
	public void loadRuat(ActionEvent actionEvent){
		RuatRegisterDTO ruatRegisterDTO = userOwnerService.findOneIdTypeIdNumber(aSession.getSelectedRuatRegister().getIdType(), aSession.getSelectedRuatRegister().getIdNumber());
		aSession.setSelectedRuatRegister(ruatRegisterDTO);
	}
	
	private Map<String,Long> getIdsProducts() {
		
		options  =new LinkedHashMap<String, Long>();
		
		for(AgrarianResourceRegisterDTO agrarianResourceRegister: aSession.getAgrarianResource().getAgrarianResourceRegisters()){
			options.put(agrarianResourceRegister.getProductDesc(),agrarianResourceRegister.getProduct());
		}

		for(ForestResourceRegisterDTO forestResourceRegister:aSession.getForestResource().getForestResourceRegisters()){
			options.put(forestResourceRegister.getProductDesc(),forestResourceRegister.getProduct());
		}
		
		for(LivestockResourceRegisterDTO livestockResourceRegister:aSession.getLivestockResource().getLivestockResourceRegisters()){
			options.put(livestockResourceRegister.getProductDesc(),livestockResourceRegister.getProduct());
		}		
		return options;
	}
}
